package br.com.jsfprimefaces.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;

import br.com.jsfprimefaces.model.Pessoa;
import br.com.jsfprimefaces.util.BancoUtil;

/**
 * @author Ramonsv89@gmail.com
 *
 */

public class PessoaDAO implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	public void insert(Pessoa pessoa){
		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		session.save(pessoa);
		session.getTransaction().commit();
		session.close();
	}
	
	public void delete(Pessoa pessoa){
		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		session.delete(pessoa);
		session.getTransaction().commit();
		session.close();
	}

	public void update(Pessoa pessoa){
		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		session.saveOrUpdate(pessoa);
		session.getTransaction().commit();
		session.close();	
	}
	
	public List<Pessoa> search(int id){
		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		
		String sql = "from pessoa where id = "+id;
		List<Pessoa> list = session.createSQLQuery(sql).list();
				
		return list;
	}
	
}
